package org.example.test;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.*;

public class MyTest03 {
    public static void main(String[] args) {
        ExecutorService executorService = Executors.newFixedThreadPool(5);
        List<MyCallable> callables = new ArrayList<>();
        for (int i = 0; i < 5000; i++) {
            MyCallable callable = new MyCallable();
            callable.setIndex(i);
            callables.add(callable);
        }
        try {
            List<Future<String>> futureList = executorService.invokeAll(callables);
            executorService.shutdown();
            boolean b = executorService.awaitTermination(1L, TimeUnit.SECONDS);
            if (!b){
                System.out.println("线程池执行未完成");
                return;
            }else{
                System.out.println("线程池执行完成");
            }
            System.out.println("线程池获取结果数："+futureList.size());
            for (Future<String> future : futureList) {
                String s;
                try {
                    s = future.get();
                    System.out.println(s);
                } catch (ExecutionException e) {
                    throw new RuntimeException(e);
                }
            }
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
